/**
 * Source file for the {@link fr.prolavage.domain.beans.TypeVehicule} type.
 * 
 * This source file belongs to a software project managed by the
 * Prolavage SARL company located in FRANCE.
 * Prolavage operates a truck washing station.
 *
 * This software project helps Prolavage manages its enterprise resources.
 * The main features consists in printing a ticket for each customer
 * with the details of the vehicle washing service to be provided.
 *
 * Copyright (C) 2014 Prolavage SARL
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * This software project MUST comply with the law of FRANCE, and with
 * the law and regulations of the European Union.
 * Since this software project is provided without any warranty,
 * it MAY NOT comply with the law of any other country.
 *
 * This source file MAY contain text written in the english and/or french languages.
 * For legal considerations, the french language SHOULD take precedence.
 *
 * This software project is hosted on https://code.google.com/p/prolavage/
 * Do not hesitate to visit our blog as well: http://prolavage.fr
 *
 * We can be contacted by postmail using the following address:
 *
 * Pro Lavage Poids Lourds SARL
 * ZI du Cormier – Bd du Cormier
 * 49300 CHOLET
 * FRANCE
 *
 */

package fr.prolavage.domain.beans;

import java.io.IOException;
import java.util.Set;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.gson.stream.JsonWriter;

/**
 * Un type de véhicule, qu'il soit général ou spécifique.
 * 
 * @author Adrien COLOM
 * 
 */
public class TypeVehicule extends Designable {

   private static final String JSON_IDS_COUPLAGES = "idsCouplages";

   private static final long serialVersionUID = -4219468845160179543L;

   private Set<CouplageTypeVehicule> couplages;

   /**
    * @see fr.prolavage.domain.beans.Designable#equals(java.lang.Object)
    */
   @Override
   public boolean equals(final Object obj) {

      if (!(obj instanceof TypeVehicule)) {
         return false;
      }

      final TypeVehicule autre = (TypeVehicule) obj;
      final EqualsBuilder equalsBuilder = new EqualsBuilder();
      equalsBuilder.appendSuper(super.equals(obj));
      equalsBuilder.append(getCouplages().size(), autre.getCouplages().size());

      return equalsBuilder.build();
   }

   /**
    * Getter.
    * 
    * @return Le couplages.
    */
   public Set<CouplageTypeVehicule> getCouplages() {

      if (null == couplages) {
         return ImmutableSet.of();
      }

      return couplages;
   }

   /**
    * @see fr.prolavage.domain.beans.Entite#hashCode()
    */
   @Override
   public int hashCode() {

      final HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
      hashCodeBuilder.appendSuper(super.hashCode());

      for (final CouplageTypeVehicule couplage : getCouplages()) {
         hashCodeBuilder.append(couplage.getDesignation());
      }

      return hashCodeBuilder.build();
   }

   /**
    * @see fr.prolavage.domain.beans.Designable#serializeToJSON(com.google.gson.stream.JsonWriter)
    */
   @Override
   protected void serializeToJSON(final JsonWriter jsonWriter) throws IOException {

      super.serializeToJSON(jsonWriter);

      final Set<Integer> idsCouplages = ImmutableSortedSet.copyOf(Entite.toSetOfIds(getCouplages()));
      jsonWriter.name(JSON_IDS_COUPLAGES);
      jsonWriter.beginArray();

      for (final Integer idCouplage : idsCouplages) {
         jsonWriter.value(idCouplage.intValue());
      }

      jsonWriter.endArray();
   }

   /**
    * Setter.
    * 
    * @param couplages
    *           les couplages.
    */
   public void setCouplages(final Set<CouplageTypeVehicule> couplages) {

      this.couplages = couplages;
   }

}
